package com.dayuanit.dy15.atm.controller;

import com.dayuanit.dy15.atm.dto.CardDTO;
import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.service.CardService;
import com.dayuanit.dy15.atm.service.FlowService;
import com.dayuanit.dy15.atm.service.impl.FlowServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class CardController extends BaseController {

    public CardController() {
        System.out.println("CardController()");
    }

    @Autowired
    private CardService cardService;

    @Autowired
    private FlowService flowService;

    /**
     * 开户
     * @return
     */
    @RequestMapping(value = "/card/openaccount.do")
    public ResponseDTO openaccount(@RequestParam("password2") String password,
                                   @RequestParam("xxxxx") String confirmPassword) {
        cardService.openaccount(1000L, password, confirmPassword);
        return ResponseDTO.sucess();
    }

    /**
     * 取款
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/card/draw.do")
    public ResponseDTO draw(HttpServletRequest request, HttpServletResponse response) {
        String cardId = request.getParameter("cardId");
        String password = request.getParameter("password");
        String amount = request.getParameter("amount");


        cardService.withdraw(Long.valueOf(cardId), password, amount);
        return ResponseDTO.sucess();
    }

    /**
     * 存款
     * @return
     */
    @RequestMapping(value = "/card/deposit.do")
    public ResponseDTO deposit(long cardId, String password, String amount) {
        try {
            cardService.deposit(cardId, password, amount);
            return ResponseDTO.sucess();
        } catch (BizException be) {
            return ResponseDTO.fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.fail("系统错误，请联系客服");
        }
    }

    @RequestMapping(value = "/card/listMyCard.do")
    public ResponseDTO listMyCard(HttpServletRequest request, HttpServletResponse response) {
        List<CardDTO> dtos = cardService.queryCards(getUserId(request));
        return ResponseDTO.sucess(dtos);
    }

}
